<!--注册界面
作者：张家豪
时间：2020/6/22
-->
<template>
  <el-dialog :title="title"  :visible="visible" :close-on-click-modal="false"
             :before-close="handleClose" width="500px">
    <el-form :model="formData" :rules="rules" ref="formData" label-width="80px">
      <el-form-item label="用户名" prop="username" >
        <el-input v-model="formData.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="formData.password"></el-input>
      </el-form-item>
       <el-form-item label="昵称" prop="nickname">
        <el-input v-model="formData.nickname"></el-input>
      </el-form-item>
      <el-form-item label="身份" prop="role">
        <el-select v-model="formData.role" style="width: 100%" clearable placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="text" @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleSubmit"
                 :loading="loading">{{loading?'提交中...':'确认提交'}}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  //import {getAllRoleList} from "../../../api/modules/role";
    import {register} from "@/api/modules/user"
  
  export default {
    name: "edit",
    props: {
      visible: {
        type: Boolean
      },
      title: {
        type: String,
        default: '新增'
      },

      defaultFormData: {
        type: Object
      },
    },
    data() {
      return {
        options:[{
            value:"老师",
            label:"老师"
            },
            {
            value:"学生",
            label:"学生"
            }],
 


        loading: false,
        
        formData: {
          title: '',
          admin: ''
        },
        address:[],
        rules: {
          title: [
            {required: true, message: "楼栋名不能为空", trigger: "blur"}
          ],
          admin: [
            {required: true, message: "管理员不能为空", trigger: "blur"}
          ]
        },
        
        refresh: false
      }
    },
    mounted() {


      if (JSON.stringify(this.defaultFormData) == "{}") {

      }else{
        let formData = this.clone(this.defaultFormData)
        this.formData = JSON.parse(JSON.stringify(formData))
      }
    },
    methods: {
        handleChange(value) {
          console.log(value);
        },
       handleClose() {
         this.$emit('close', this.refresh)
       },
       handleSubmit() {
         this.$refs.formData.validate(valid => {
           if (valid) {
             this.loading = true
             this.formData.adress = this.address.join('/');
             console.log(this.formData.adress)
               register({
                 ...this.formData,
               }).then(res => {
                 this.loading = false
                 if (res.success) {
                   this.refresh = true
                   this.toast(res.message)
                   this.handleClose()
                 }
               }).catch(data =>{
                    this.toast(data,4)
                    this.refresh = true
                    this.handleClose()
                }) 
           
           }
         })
       }
    }
  }
</script>

